/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package events.SquashEvent;
 
import com.l2jserver.gameserver.instancemanager.QuestManager;
import com.l2jserver.gameserver.model.L2Object;
import com.l2jserver.gameserver.model.L2Skill;
import com.l2jserver.gameserver.model.actor.L2Npc;
import com.l2jserver.gameserver.model.actor.instance.L2MonsterInstance;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.model.quest.Quest;
import com.l2jserver.gameserver.model.quest.QuestState;
import com.l2jserver.gameserver.network.clientpackets.Say2;
import com.l2jserver.gameserver.network.serverpackets.Attack;
import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
import com.l2jserver.util.Rnd;
 
/**
 * @author Gnacik
 * @version 1.0
 * @see Warning! Mostly that event is custom!
 */
public class SquashEvent extends Quest
{
    private static final int MANAGER = 31860;
   
    private static final int NECTAR_SKILL = 2005;
   
    private static final int[] CHRONO_LIST = {
        4202,5133,5817,7058,8350
    };
   
    private static final int[] SQUASH_LIST = {
        12774,12775,12776,
        12777,12778,12779,
        13016,13017
    };
   
    private static final String[] _NOCHRONO_TEXT = {
        "No puedes matarme sin una Chrono",
        "Jejeje...sigue sigue...",
        "Buen intento...",
        "No te cansas ?",
        "Jajajjaj ! Mas fuerte ..."
    };
   
    private static final String[] _CHRONO_TEXT = {
        "Arghh... una Chrono...",
        "Mi final se acerca ...",
        "por favor dejame !",
        "Ayuda !! ...",
        "Que alguien me ayude !!..."
    };
    private static final String[] _NECTAR_TEXT = {
        "Hummm... Nectar...",
        "Si! Dame mas!...",
        "Hmmm.. Mas.. necesito mas!...",
        "Sigue, Sigue! Sigue! OH! SIGUE!...",
        "Hmmmmmmm...",
        "Es mi elixir favorito !..."
    };
   
    private static final int[][] DROPLIST =
    {
        // must be sorted by npcId !
        // npcId, itemId, chance, min, max
       
    //YOUNGS    
        // Young Squash
        { 12774,  1060, 100, 1, 5 },    // Lesser Healing potion
        { 12774,  1062,  50, 1, 2 },    // Haste potion
        // High Quality Squash
        { 12775,  1539, 100, 1, 10 },   // Greater Healing potion
        { 12775,  1375,  70, 1, 4 },    // Greater Swift Attack Potion
        { 12775,  1459,  50, 5, 15 },   // Crystal c-grade
        { 12775, 13021, 15, 1, 2 },     //Color title
        { 12775, 4037, 15, 1, 20 },     //Moneditas
        { 12775, 951, 5, 1, 1 },        //EWC
        { 12775, 952, 5, 1, 1 },        //EAC
        { 12775, 948, 2, 1, 1 },        //EAB
        { 12775, 20393, 10, 1, 1 },     //Sweet Fruit
        { 12775, 20394, 10, 1, 1 },     //Fresh Fruit
        { 12775, 13022 , 1, 1, 1 },     //Caballo
        // Low Quality Squash
        { 12776,  1061, 100, 1, 5 },    // Healing potion
        { 12776,  1062,  70, 1, 3 },    // Haste potion
        { 12776,  1458,  50, 5, 7 },    // Crystal d-grade
        { 12776, 13021, 5, 1, 1 },      //Color title
    //LARGE
        // Large Young Squash
        { 12777,  1061, 100, 1, 5 },    // Healing potion
        { 12777,  1374,  50, 1, 2 },    // Greater Haste potion
        // High Quality Large
        { 12778,  1539, 100, 1, 10 },   // Greater Healing potion      
        { 12778,  6036,  70, 1, 3 },    // Greater Magic Haste Potion
        { 12778,  1459,  40, 15, 30 },  // Crystal c-grade
        { 12778, 13021, 20, 1, 4 },     //Color title
        { 12778, 4037, 18, 1, 30 },     //Moneditas
        { 12778, 947, 7, 1, 1 },        //EWB
        { 12778, 948, 7, 1, 1 },        //EAB
        { 12778, 20393, 15, 1, 2 },     //Sweet Fruit
        { 12778, 20394, 15, 1, 2 },     //Fresh Fruit
        // Low Quality Large
        { 12779,  6035,  70 , 1 , 3 },  // Magic Haste Potion
        { 12779,  1458,  50 , 5 , 11 }, // Crystal d-grade
        { 12779, 13021, 12, 1, 2 },     //Color title
        { 12779, 4037, 12, 1, 10 },     //Moneditas
        { 12779, 951, 8, 1, 1 },        //EAC

       
    //SUPER
        // King
        { 13016,  1540, 100 , 3 , 8 },  // Quick Healing Potion
        { 13016,  1460,  40 , 4 , 8 },  // Crystal b-grade
        { 13016,  5234,  20 , 1 , 1 },  // Mystery Potion
        { 13016, 13021, 8, 3, 5 },      //Color title
        { 13016, 4037, 20, 15, 35 },    //Moneditas
        { 13016, 20393, 20, 1, 3 },     //Sweet Fruit
        { 13016, 20394, 20, 1, 3 },     //Fresh Fruit
        { 13016, 729, 5, 1, 1 },        //EWA
        { 13016, 730, 5, 1, 1 },        //EAA
        // Emperor
        { 13017,  1540, 100 , 5 , 10 }, // Quick Healing Potion
        { 13017, 20004,  40 , 1 , 2 },  // Energy Ginseng
        { 13017,  1461,  20 , 3 , 6 },  // Crystal a-grade
        { 13017,  5234,  10 , 1 , 1 },  // Mystery Potion
        { 13017, 13021, 12, 3, 5 },     //Color title
        { 13017, 4037, 25, 20, 40 },    //Moneditas
        { 13017, 20393, 25, 2, 3 },     //Sweet Fruit
        { 13017, 20394, 25, 2, 3 },     //Fresh Fruit
        { 13017, 729, 7, 1, 1 },        //EWA
        { 13017, 730, 7, 1, 1 },        //EAA
        { 13017, 960, 1, 1, 1 },        //EAS
        { 13017, 959, 1, 1, 1 },        //EWS
       
       
    };
   
    public String onAttack(L2Npc npc, L2PcInstance attacker, int damage, boolean isPet, L2Skill skill)
    {      
       
        if (contains(SQUASH_LIST,npc.getNpcId()))
        {
            if(isPet)
            {
                noChronoText(npc);
                npc.setIsInvul(true);              
                return null;
            }
            if(attacker.getActiveWeaponItem() != null && skill == null && contains(CHRONO_LIST,attacker.getActiveWeaponItem().getItemId()))
            {
                ChronoText(npc);
                npc.setIsInvul(false);
                npc.getStatus().reduceHp(damage, attacker);
                return null;
            }
           
            else
            {
                noChronoText(npc);
                npc.setIsInvul(true);
                return null;
            }
        }
        return super.onAttack(npc, attacker, damage, isPet);
    }
   
    public String onSkillSee(L2Npc npc, L2PcInstance caster, L2Skill skill, L2Object[] targets, boolean isPet)
    {
        if (contains(targets,npc) && contains(SQUASH_LIST,npc.getNpcId()) && (skill.getId() == NECTAR_SKILL))
        {
            switch(npc.getNpcId())
            {
                case 12774:
                    randomSpawn(12775, 12776, npc, true);
                    break;
                case 12777:
                    randomSpawn(12778, 12779, npc, true);
                    break;
                case 12775:
                    randomSpawn(13016, npc, true);
                    break;
                case 12778:
                    randomSpawn(13017, npc, true);
                    break;
            }
            return super.onSkillSee(npc,caster,skill,targets,isPet);
        }
        else {
            return null;
        }
    }
 
    public String onKill (L2Npc npc, L2PcInstance killer, boolean isPet)
    {
        dropItem(npc, killer);
       
        return super.onKill(npc, killer, isPet);
    }
 
    public String onSpawn(L2Npc npc)
    {
        npc.setIsImmobilized(true);
        npc.disableCoreAI(true);
        return null;
        // return super.onSpawn(npc);
    }
   
    private static final void dropItem(L2Npc mob, L2PcInstance player)
    {
        final int npcId = mob.getNpcId();
        final int chance = Rnd.get(100);
        for (int i = 0; i < DROPLIST.length; i++)
        {
            int[] drop = DROPLIST[i];
            if (npcId == drop[0])
            {
                if (chance < drop[2])
                {
                    if(drop[1] > 1)
                        ((L2MonsterInstance)mob).dropItem(player, drop[1], Rnd.get(drop[3], drop[4]));
                    else
                        ((L2MonsterInstance)mob).dropItem(player, drop[1], Rnd.get(drop[3], drop[4]));
                    continue;
                }
            }
            if (npcId < drop[0])
                return; // not found
        }
    }
   
    private void randomSpawn(int lower, int higher, L2Npc npc, boolean delete)
    {
        int _random = Rnd.get(100);
        if (_random < 10)
            spawnNext(lower, npc);
        else if(_random < 30)
            spawnNext(higher, npc);
        else
            nectarText(npc);
    }
   
    private void randomSpawn(int npcId, L2Npc npc, boolean delete)
    {
        if(Rnd.get(100) < 10)
            spawnNext(npcId, npc);
        else
            nectarText(npc);
    }
   
    private void ChronoText(L2Npc npc)
    {
        if(Rnd.get(100) < 20)
            npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _CHRONO_TEXT[Rnd.get(_CHRONO_TEXT.length)]));      
    }
    private void noChronoText(L2Npc npc)
    {
        if(Rnd.get(100) < 20)
            npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _NOCHRONO_TEXT[Rnd.get(_NOCHRONO_TEXT.length)]));      
    }
    private void nectarText(L2Npc npc)
    {
        if(Rnd.get(100) < 30)
            npc.broadcastPacket(new CreatureSay(npc.getObjectId(), Say2.ALL, npc.getName(), _NECTAR_TEXT[Rnd.get(_NECTAR_TEXT.length)]));
    }
   
    private void spawnNext(int npcId, L2Npc npc)
    {
        addSpawn(npcId, npc.getX(), npc.getY(), npc.getZ(), npc.getHeading(), false, 180000);      
        npc.deleteMe();
    }
 
    public static <T> boolean contains(T[] array, T obj)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] == obj)
            {
                return true;
            }
        }
        return false;
    }
   
    public static boolean contains(int[] array, int obj)
    {
        for (int i = 0; i < array.length; i++)
        {
            if (array[i] == obj)
            {
                return true;
            }
        }
        return false;
    }
   
    public SquashEvent(int questId, String name, String descr)
    {
        super(questId, name, descr);
       
        for (int mob : SQUASH_LIST)
        {
            addAttackId(mob);
            addKillId(mob);
            addSpawnId(mob);
            addSkillSeeId(mob);
        }
 
        addStartNpc(MANAGER);
        addFirstTalkId(MANAGER);
        addTalkId(MANAGER);
       
        addSpawn(MANAGER, 83077, 147910, -3471, 29412, false, 0);
    }
   
    @Override
    public String onFirstTalk(L2Npc npc, L2PcInstance player)
    {
        String htmltext = "";
        QuestState st = player.getQuestState(getName());
        if (st == null)
        {
            Quest q = QuestManager.getInstance().getQuest(getName());
            st = q.newQuestState(player);
        }
        htmltext = npc.getNpcId() + ".htm";
        return htmltext;
    }
   
    public static void main(String[] args)
    {
        new SquashEvent(-1,"SquashEvent","events");
    }
}